package com.example.demo.controller;

import com.example.demo.entity.Userinfo;
import com.example.demo.service.LogService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-06
 * Time: 11:44
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    //事务管理器
    @Resource
    private DataSourceTransactionManager transactionManager;

    //定义事务属性
    @Resource
    private TransactionDefinition transactionDefinition;

    @RequestMapping("/getUser")
    public Integer getUser(Userinfo userinfo) {
        //判断前端传过来的参数是否为空
        if (userinfo == null ||
                !StringUtils.hasLength(userinfo.getUsername()) ||
                !StringUtils.hasLength(userinfo.getPassword())) {
            return 0;
        }

        //开启事务
        TransactionStatus transactionStatus =
                transactionManager.getTransaction(transactionDefinition);
        userinfo.setCreatetime(LocalDateTime.now().toString());
        userinfo.setUpdatetime(LocalDateTime.now().toString());

        int result = userService.getUser(userinfo);
        //进行事务的提交
        transactionManager.commit(transactionStatus);

        //进行事务的回滚
//        transactionManager.rollback(transactionStatus);
        return result;
    }

    //使用注解进行事务的提交，回滚操作
    @Transactional(propagation = Propagation.NESTED)  //这里是嵌套事务，临时工，异常之后不会回滚事务
    @RequestMapping("/insert")
    public Integer Insert(Userinfo userinfo) {
        if (userinfo == null ||
                !StringUtils.hasLength(userinfo.getUsername()) ||
                !StringUtils.hasLength(userinfo.getPassword())) {
            return 0;
        }
        userinfo.setCreatetime(LocalDateTime.now().toString());
        userinfo.setUpdatetime(LocalDateTime.now().toString());
        int result = userService.getUser(userinfo);
        //这里会抛异常，看事务是否会回滚   加入事务，会自动回滚
        if(result > 0){
            logService.add();
        }

        //遇到异常事务会自动回滚  可以选择将异常自行处理，事务就不进行回滚，也可以将异常继续抛出
//        try {
//            int ret = 10 / 0;
//        } catch (Exception e) {
//            throw(e);
//            System.out.println(e.getMessage());  //此时遇到异常自行处理，但是事务并没有回滚，选择使用代码进行事务的回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
        return result;
    }
}
